package com.ruoyi.web.service.impl;

import com.ruoyi.web.api.domain.TChapterCatagory;
import com.ruoyi.web.api.domain.TChapter;
import com.ruoyi.web.api.domain.TChapterCatagoryVo;
import com.ruoyi.web.mapper.TChapterCatagoryMapper;
import com.ruoyi.web.mapper.TChapterMapper;
import com.ruoyi.web.service.ITChapterCatagoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-08
 */
@Service
public class TChapterCatagoryServiceImpl implements ITChapterCatagoryService 
{
    @Autowired
    private TChapterCatagoryMapper tChapterCatagoryMapper;

    @Autowired
    private TChapterMapper tChapterMapper;

    /**
     * 查询文章分类
     * 
     * @param ccid 文章分类主键
     * @return 文章分类
     */
    @Override
    public TChapterCatagory selectTChapterCatagoryByCcid(Long ccid)
    {
        return tChapterCatagoryMapper.selectTChapterCatagoryByCcid(ccid);
    }

    @Override
    public List<TChapterCatagoryVo> selectTChapterCatagoryAndChapters( ) {
        List<TChapterCatagory> catagoryList = tChapterCatagoryMapper.selectTChapterCatagoryList(null);
        List<TChapterCatagoryVo> chapterCatagoryVoList = catagoryList.stream().map(item -> {
            TChapterCatagoryVo tChapterCatagoryVo = new TChapterCatagoryVo();
            TChapter tChapter = new TChapter();
            tChapter.setCcid(item.getCcid());
            List<TChapter> tChapters = tChapterMapper.selectTChapterList(tChapter);
            tChapterCatagoryVo.settChapterList(tChapters);
            BeanUtils.copyProperties(item, tChapterCatagoryVo);
            return tChapterCatagoryVo;
        }).collect(Collectors.toList());
        return chapterCatagoryVoList;
    }

//    @Override
//    public List<TChapterCatagoryVo> selectTChapterCatagoryAndChapters() {
//        List<TChapterCatagory> catagoryList = tChapterCatagoryMapper.selectTChapterCatagoryList(null);
//        List<TChapterCatagoryVo> chapterCatagoryVoList = new ArrayList<>();
//        for (TChapterCatagory item  : catagoryList) {
//            TChapterCatagoryVo tChapterCatagoryVo = new TChapterCatagoryVo();
//            TChapter tChapter = new TChapter();
//            tChapter.setCcid(item.getCcid());
//
//            List<TChapter> tChapters = tChapterMapper.selectTChapterList(tChapter);
//            tChapterCatagoryVo.settChapterList(tChapters);
//            BeanUtils.copyProperties(item, tChapterCatagoryVo);
//            chapterCatagoryVoList.add(tChapterCatagoryVo);
//        }
//        return chapterCatagoryVoList;
//    }

    /**
     * 查询文章分类列表
     * 
     * @param tChapterCatagory 文章分类
     * @return 文章分类
     */
    @Override
    public List<TChapterCatagory> selectTChapterCatagoryList(TChapterCatagory tChapterCatagory)
    {
        return tChapterCatagoryMapper.selectTChapterCatagoryList(tChapterCatagory);
    }

    /**
     * 新增文章分类
     * 
     * @param tChapterCatagory 文章分类
     * @return 结果
     */
    @Override
    public int insertTChapterCatagory(TChapterCatagory tChapterCatagory)
    {
        return tChapterCatagoryMapper.insertTChapterCatagory(tChapterCatagory);
    }

    /**
     * 修改文章分类
     * 
     * @param tChapterCatagory 文章分类
     * @return 结果
     */
    @Override
    public int updateTChapterCatagory(TChapterCatagory tChapterCatagory)
    {
        return tChapterCatagoryMapper.updateTChapterCatagory(tChapterCatagory);
    }

    /**
     * 批量删除文章分类
     * 
     * @param ccids 需要删除的文章分类主键
     * @return 结果
     */
    @Override
    public int deleteTChapterCatagoryByCcids(Long[] ccids)
    {
        return tChapterCatagoryMapper.deleteTChapterCatagoryByCcids(ccids);
    }

    /**
     * 删除文章分类信息
     * 
     * @param ccid 文章分类主键
     * @return 结果
     */
    @Override
    public int deleteTChapterCatagoryByCcid(Long ccid)
    {
        return tChapterCatagoryMapper.deleteTChapterCatagoryByCcid(ccid);
    }
}
